package com.fjwt.gz.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.db.entity.MchChannelEntity;
import com.fjwt.gz.db.entity.MchInfoEntity;
import com.fjwt.gz.db.vo.MchChannelVO;
import com.fjwt.gz.db.vo.MchInfoVO;
import com.fjwt.gz.service.mapper.MchChannelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 进件商户三方间联号表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class MchChannelService extends ServiceImpl<MchChannelMapper, MchChannelEntity> {

    @Autowired
    private MchInfoService mchInfoService;

    /**
     * 查询进件商户三方间联号表列表
     *
     * @param iPage
     * @param mchNo
     * @param ifName
     * @return
     */
    public IPage<MchChannelVO> queryMchChannelList(IPage iPage, String mchNo, String ifName) {
        return this.baseMapper.queryMchChannelList(iPage, mchNo, ifName);
    }

    /**
     * 根据星级查询商户名单
     * @return
     */
    public List<MchChannelEntity> getMchNoList(List<Integer> levelList, int agentLevel, String agentNo) {
        LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper = new LambdaQueryWrapper<>();
        mchChannelWrapper.in(MchChannelEntity::getStar, levelList);
        mchChannelWrapper=filterAgentNo(mchChannelWrapper,agentLevel,agentNo);

        List<MchChannelEntity> mchChannelList = list(mchChannelWrapper);
        return mchChannelList;
    }

    /**
     * oss文件导入商户名单
     * @return
     */
    public List<MchChannelEntity> getMchNoList(String ossFileUrl, int agentLevel, String agentNo) {
        List<MchChannelEntity> mchChannelList = new ArrayList<>();
        File file = FileUtil.file(String.format("./temp/%s.xlsx", DateUtil.date().getTime()));
        HttpUtil.downloadFile(ossFileUrl, file);
        ExcelReader reader = ExcelUtil.getReader(file);
        reader.addHeaderAlias("商户号", "mchNo");
        reader.addHeaderAlias("商户名称", "mchName");
        List<MchInfoVO> all = reader.readAll(MchInfoVO.class);
        all.forEach(item -> {
            LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper = new LambdaQueryWrapper<>();
            mchChannelWrapper.eq(MchChannelEntity::getMchNo, item.getMchNo());
            mchChannelWrapper=filterAgentNo(mchChannelWrapper,agentLevel,agentNo);
            MchChannelEntity one = getOne(mchChannelWrapper);
            if (one != null) {
                mchChannelList.add(one);
            }
        });
        FileUtil.del(file);
        return mchChannelList;
    }

    /**
     * 根据进件时间查询商户名单
     * @return
     */
    public List<MchChannelEntity> getMchNoList(Date applyDate, int agentLevel, String agentNo) {
        LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper = new LambdaQueryWrapper<>();
        mchChannelWrapper.ge(MchChannelEntity::getApplyDate, applyDate);
        mchChannelWrapper=filterAgentNo(mchChannelWrapper,agentLevel,agentNo);

        List<MchChannelEntity> mchChannelList = list(mchChannelWrapper);
        return mchChannelList;
    }

    /**
     * 全量查询商户名单
     * @return
     */
    public List<MchChannelEntity> getMchNoList(int agentLevel, String agentNo) {
        LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper = new LambdaQueryWrapper<>();
        mchChannelWrapper=filterAgentNo(mchChannelWrapper,agentLevel,agentNo);

        List<MchChannelEntity> mchChannelList =  list(mchChannelWrapper);
        return mchChannelList;
    }

    /**
     * 根据进件周期，上个月商户进件时间查询商户名单
     * @return PERIOD
     */
    public List<MchChannelEntity> getMchNoList(int periodType, int agentLevel, String agentNo) {
        DateTime lastMonthDate = DateUtil.lastMonth();//当前日期，上一月时间
        DateTime lastMonthEndDate = DateUtil.endOfMonth(lastMonthDate);//当前日期，上一月月末
        DateTime lastMonthStartDate = DateUtil.beginOfMonth(lastMonthDate);//当前日期，上一月月首
        LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper = new LambdaQueryWrapper<>();
        mchChannelWrapper.ge(MchChannelEntity::getApplyDate, lastMonthStartDate);
        mchChannelWrapper.le(MchChannelEntity::getApplyDate, lastMonthEndDate);
        mchChannelWrapper=filterAgentNo(mchChannelWrapper,agentLevel,agentNo);

        List<MchChannelEntity> mchChannelList = list(mchChannelWrapper);
        return mchChannelList;
    }

    /**
     *  筛选条件加agentNo
     * @param mchChannelWrapper
     * @param agentLevel
     * @param agentNo
     * @return
     */
    private LambdaQueryWrapper<MchChannelEntity> filterAgentNo(LambdaQueryWrapper<MchChannelEntity> mchChannelWrapper, int agentLevel, String agentNo) {
        LambdaQueryWrapper<MchInfoEntity> mchInfoQueryWrapper = MchInfoEntity.gw();
        if (mchChannelWrapper == null) return null;
        switch (agentLevel) {
            case 2:
                mchInfoQueryWrapper.eq(MchInfoEntity::getAgentNo2, agentNo);
                break;
            case 3:
                mchInfoQueryWrapper.eq(MchInfoEntity::getAgentNo3, agentNo);
                break;
            case 1:
                mchInfoQueryWrapper.eq(MchInfoEntity::getAgentNo4, agentNo);
                break;
        }
        return mchChannelWrapper.in(MchChannelEntity::getMchNo, mchInfoService.list(mchInfoQueryWrapper));

    }
}
